//    Travel US - For users of travel.usairways.com
//    intended for US Airways employees and their dependents
//    This app will help you track your flights and display
//    Availability, flight status and other non-rev info
//    Copyright (C) 2014  Renats Stozkovs
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

package com.bc004346.travelus.DataAccess.AsyncTasks;

import android.app.Activity;
import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;

import com.bc004346.travelus.DataAccess.TravelUsScreenScraper;
import com.bc004346.travelus.Model.Interface.IGetNoticesCallback;
import com.bc004346.travelus.Utility.Utility;

import java.util.List;

import static com.bc004346.travelus.Utility.Utility.log;

/**
 * Created by rstozkov on 1/28/14.
 * Handles refreshing reservation data from Travel US
 */
public class GetNoticesTaskFragment extends Fragment {

    //<editor-fold desc="Members">

    private TravelUsScreenScraper mTravelUs;
    private IGetNoticesCallback mCallbacks;

    //</editor-fold>

    //<editor-fold desc="Overrides">

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mCallbacks = (IGetNoticesCallback) activity;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
        mTravelUs = Utility.getInstance().getTravelUS();
        new GetReservationsTask().execute();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mCallbacks = null;
    }

    //</editor-fold>

    private class GetReservationsTask extends AsyncTask<Void, Void, List<String>> {

        private Exception ex = null;

        @Override
        protected void onPostExecute(List<String> notices) {

            if (mCallbacks != null) {
                mCallbacks.onGetProfilePostExecute(notices, ex);
            }
        }

        @Override
        protected List<String> doInBackground(Void... params) {
            List<String> notices = null;
            try {
                notices = mTravelUs.getNotices();
            } catch (Exception e) {
                log(String.format("Exception: %s", e.toString()));
                ex = e;
            }
            return notices;
        }
    }
}
